//
//  LHRecommendHotelTableView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-16.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendHotelTableView.h"
#import "LHRecommendHotelTableViewCell.h"
#import "LHNetworkHandle.h"
#import "LHRecommendHotelModel.h"
#import "UIScrollView+MJRefresh.h"

@interface LHRecommendHotelTableView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *hotelTableView;
@property (nonatomic, retain) NSMutableArray *hotelArr;
@property (nonatomic, retain) NSMutableArray *tempArr;

@end

@implementation LHRecommendHotelTableView

- (void)dealloc
{
    
    [_hotelTableView release];
    [_hotelArr release];
    [_tempArr release];
    [super dealloc];
}

// 初始化
- (instancetype)initWithFrame:(CGRect)frame cityId:(NSString *)cityId
{
    if (self = [super initWithFrame:frame]) {
        
        self.tempArr = [NSMutableArray array];
        
        [self getDataWithCityId:cityId];
    }
    
    return self;
}

/**
 *  创建推荐旅店tableView
 */
- (void)createHotelTableView
{
    self.hotelTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    
    [_hotelTableView addFooterWithTarget:self action:@selector(tempData)];
    [_hotelTableView setFooterRefreshingText:@"客官等一等"];
    
    [_hotelTableView addHeaderWithTarget:self action:@selector(tempTableView)];
    [_hotelTableView setHeaderRefreshingText:@"我正在用力~~~"];
    
    _hotelTableView.delegate = self;
    _hotelTableView.dataSource = self;
    
    [self addSubview:_hotelTableView];
    
    [_hotelTableView release];
    
}

#pragma mark- tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tempArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommendHotelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hh"];
    
    if (cell == nil) {
        
        cell = [[LHRecommendHotelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hh"];
    }
    
    cell.recHotelModel = _tempArr[indexPath.item];
    
    cell.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.width / 3 + 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.delegate jumpToHotelDetailWithIndexPath:indexPath hotelArr:_tempArr];
    
}

// 获取网络数据
- (void)getDataWithCityId:(NSString *)cityId
{
    NSString *h = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/%@/pois/hotel/?start=0&count=40&sort=default&shift=false&latitude=38.8826495629487&longitude=121.53969115009306", cityId];
    
    [LHNetworkHandle getDataWithURL:h completion:^(id result) {
        
        self.hotelArr = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"items"];
        
        for (NSDictionary *tempDic in arr) {
            
            LHRecommendHotelModel *hotelModel = [[LHRecommendHotelModel alloc] initWithDic:tempDic];
            
            [_hotelArr addObject:hotelModel];
            
            [hotelModel release];
            
        }
        
        for (NSInteger i = 0; i < 10; i++) {
            
            [self.tempArr addObject:self.hotelArr[i]];
            
        }
        
        [self createHotelTableView];
        
    }];
}

- (void)tempData
{
    if (self.tempArr.count != self.hotelArr.count) {
        
        NSInteger j = self.tempArr.count;
        
        for (NSInteger i = j; i < j + 5; i++) {
            
            [self.tempArr addObject:self.hotelArr[i]];
        }
        
        
    }
    
    [_hotelTableView reloadData];
    [_hotelTableView footerEndRefreshing];
    
}

- (void)tempTableView
{
    [_hotelTableView reloadData];
    [_hotelTableView headerEndRefreshing];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
